//
//  Table.m
//
//  Created by lee jory on 09-10-22.
//  Copyright 2009 Netgen. All rights reserved.
//

#import "Table.h"
#import "ColorFactory.h"

@implementation TableScrollView
@synthesize iTable;

@end

@implementation TableDataSource
@synthesize titles,data,columnWidth;
@synthesize iHeaderBgColor,iHeaderFontColor,iHorizontal;

-init
{
	self = [super init];
	if (self) {
		self.titles = [[NSMutableArray alloc] init];
		self.columnWidth = [[NSMutableArray alloc] init];
		self.data = [[NSMutableArray alloc] init];
	}
	return self;
}

- (void)dealloc {
	[titles release];
	[columnWidth release];
	[data release];
    [super dealloc];
}

@end

//声明私有方法
@interface Table(Private)

	/**
	 * 初始化各子视图
	 */
	-(void)layoutSubView:(CGRect)aRect;

	/**
	 * 用数据项填冲数据
	 */
	-(void)fillData;

@end


@implementation Table
@synthesize dataSource,cellHeight,vRight,vLeft;
@synthesize iAbsoluteY;

- (id)initWithFrame:(CGRect)aRect data:(TableDataSource*)aDataSource{
	//aRect.size.height -= 64;
	self = [super initWithFrame:aRect];
	if(self != nil){
				
		self.clipsToBounds = YES;
		self.dataSource = aDataSource;

		//初始显示视图及Cell的长宽高
		contentWidth = .0;
		cellHeight = 30.0;
		cellWidth = [[dataSource.columnWidth objectAtIndex:0] intValue];
		cellWidth = cellWidth * aRect.size.width / 100;
		for(int i=1;i<[dataSource.columnWidth count];i++)
			contentWidth += [[dataSource.columnWidth objectAtIndex:i] intValue];
		contentHeight = [dataSource.data count] * cellHeight;		
		contentWidth = contentWidth + [[dataSource.columnWidth objectAtIndex:0] intValue]  < aRect.size.width 
			? aRect.size.width : contentWidth;

		//初始化各视图
		[self layoutSubView:aRect];
		
		//填冲数据
		[self fillData];

	}
	return self;
}
-(void)layoutSubView:(CGRect)aRect{
	vLeftContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, contentHeight)];
	vRightContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width - cellWidth, contentHeight)];
	
	vLeftContent.opaque = YES;
	vRightContent.opaque = YES;
	
	//初始化各视图
	vTopLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight)];
	vLeft = [[TableScrollView alloc] initWithFrame:CGRectMake(0, cellHeight, aRect.size.width, aRect.size.height - cellHeight)];
	vRight = [[TableScrollView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, contentHeight)];
	vTopRight = [[UIView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, cellHeight)];
	
	vLeft.iTable = self;
	vRight.iTable = self;
	
	vLeft.opaque = YES;
	vRight.opaque = YES;
	vTopLeft.opaque = YES;
	vTopRight.opaque = YES;
	
	//设置ScrollView的显示内容
	vLeft.contentSize = CGSizeMake(aRect.size.width, contentHeight);
	vRight.contentSize = CGSizeMake(contentWidth,aRect.size.height - cellHeight);
	
	//设置ScrollView参数
	vRight.delegate = self;
	
	vTopLeft.backgroundColor = [CColorFactory CreateRgbCorol:dataSource.iHeaderBgColor];
	vTopRight.backgroundColor = [CColorFactory CreateRgbCorol:dataSource.iHeaderBgColor];
//	vRight.backgroundColor = [UIColor grayColor];
	
	//添加各视图
	[vRight addSubview:vRightContent];
	[vLeft addSubview:vLeftContent];
	[vLeft addSubview:vRight];
	[self addSubview:vTopLeft];
	[self addSubview:vLeft];
	
	[vLeft bringSubviewToFront:vRight];
	[self addSubview:vTopRight];
	[self bringSubviewToFront:vTopRight];
}


-(void)fillData{

	float columnOffset = 0.0;
	//填冲标题数据
	for(int column = 0;column < [dataSource.titles count];column++){
		float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
		columnWidth = columnWidth * self.bounds.size.width / 100;
		UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, 0, columnWidth -1, cellHeight )];
		l.font = [UIFont systemFontOfSize:12.0f];
		l.text = [dataSource.titles objectAtIndex:column];
		l.backgroundColor = [UIColor clearColor];
		l.textColor = [CColorFactory CreateRgbCorol:dataSource.iHeaderFontColor];
		switch (dataSource.iHorizontal) {
			case 0:
				l.textAlignment = UITextAlignmentCenter;
				break;
			case 1:
				l.textAlignment = UITextAlignmentLeft;
				break;
			case 2:
				l.textAlignment = UITextAlignmentRight;
				break;
		}
		

		if( 0 == column){
			[vTopLeft addSubview:l];
		}
		else{	
			[vTopRight addSubview:l];
			columnOffset += columnWidth;
		}
		[l release];
	}	

	//填冲数据内容	
	/*
	NSArray* layoutControls = NULL;
	CUIControlBase* control = NULL;
	int rowCount = [dataSource.data count];
	int controlCount = 0;
	for(int i = 0;i < rowCount; i++){
		layoutControls = [dataSource.data objectAtIndex:i];
		columnOffset = 0.0;
		controlCount = [layoutControls count];
		for(int column = 0; column < controlCount; column++){
			float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
			columnWidth = columnWidth * self.bounds.size.width / 100;
			control = [layoutControls objectAtIndex:column];
			control.tag = i * cellHeight + column + 1000;
			if( 0 == column){
				control.frame = CGRectMake(columnOffset + 2,  i * cellHeight , columnWidth -1 , cellHeight -1 );
				[vLeftContent addSubview:control];
			}
			else{
				control.frame = CGRectMake(columnOffset, i * cellHeight  , columnWidth, cellHeight -1 );
				[vRightContent addSubview:control];
				columnOffset += columnWidth;
			}
		}
	}*/
}

- (NSString*) getSelectText:(int) aIndex
{
	/*CUIControlBase* control = NULL;
	CUIControlBase* check = NULL;
	int rowCount = [dataSource.data count];
	NSArray *views = NULL;
	NSMutableString* retStr = [[[NSMutableString alloc] init] autorelease];
	for(int i = 0;i < rowCount; i++){
		views = [dataSource.data objectAtIndex:i];
		check = [views objectAtIndex:0];
		if ([check isKindOfClass:[CUICheckGroup class]]) {
			if ([(CUICheckGroup*)check getStatus:0]) {
				control = [views objectAtIndex:aIndex];
				if (control.iText) {
					[retStr appendString:control.iText];
					[retStr appendString:@","];
				}
			}
		}
	}
	if ([retStr length] > 0) {
		NSRange range = NSMakeRange([retStr length]-1,1);
		[retStr deleteCharactersInRange:range];
	}
	return retStr;*/
	return nil;
}

- (NSString*) getSelectValue:(int) aIndex
{
	/*
	CUIControlBase* control = NULL;
	CUIControlBase* check = NULL;
	int rowCount = [dataSource.data count];
	NSArray *views = NULL;
	NSMutableString* retStr = [[[NSMutableString alloc] init] autorelease];
	for(int i = 0;i < rowCount; i++){
		views = [dataSource.data objectAtIndex:i];
		check = [views objectAtIndex:0];
		if ([check isKindOfClass:[CUICheckGroup class]]) {
			if ([(CUICheckGroup*)check getStatus:0]) {
				control = [views objectAtIndex:aIndex];
				if (control.iValue) {
					[retStr appendString:control.iValue];
					[retStr appendString:@","];
				}
			}
		}
	}
	if ([retStr length] > 0) {
		NSRange range = NSMakeRange([retStr length]-1,1);
		[retStr deleteCharactersInRange:range];
	}
	return retStr;
	 */
	return nil;
}



//-------------------------------以下为事件处发方法----------------------------------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
	
	vTopRight.frame = CGRectMake(cellWidth, 0, vRight.contentSize.width, vTopRight.frame.size.height);
	vTopRight.bounds = CGRectMake(scrollView.contentOffset.x, 0, vTopRight.frame.size.width, vTopRight.frame.size.height);
	vTopRight.clipsToBounds = YES;	
	vRightContent.frame = CGRectMake(0, 0  , 
									 vRight.contentSize.width , contentHeight);
	[self addSubview:vTopRight];
	vRight.frame =CGRectMake(cellWidth, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height); 
	[vLeft addSubview:scrollView];
	
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
	scrollView.frame = CGRectMake(cellWidth, 0, scrollView.frame.size.width, self.frame.size.height);
	vRightContent.frame = CGRectMake(0, cellHeight - vLeft.contentOffset.y  , 
									 vRight.contentSize.width , contentHeight);
	
	vTopRight.frame = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
	vTopRight.bounds = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
	[scrollView addSubview:vTopRight];
	[self addSubview:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	if(!decelerate)
		[self scrollViewDidEndDecelerating:scrollView];
}

- (void) dealloc
{
	[vLeft release];
	[vRight release];
	[vRightContent release];
	[vLeftContent release];
	[vTopLeft release];
	[vTopRight release];
	[dataSource release];
	[super dealloc];
}


@end
